package mysql

import (
	"gorm.io/gorm"
)

type Product struct {
	gorm.Model
	CategoryId         int    `gorm:"type:int(10);not null;comment:'分类Id'"`
	ProductName        string `gorm:"type:varchar(20);not null;comment:'商品名称'"`
	ProductCode        string `gorm:"type:varchar(100);not null;comment:'商品编号'"`
	ProductSelling     string `gorm:"type:varchar(150);not null;comment:'商品卖点'"`
	ProductImage       string `gorm:"type:varchar(100);not null;comment:'商品图片'"`
	VideoUrl           string `gorm:"type:varchar(150);not null;comment:'商品商品'"`
	ProductStockUnitId int    `gorm:"type:int(10);not null;comment:'商品库存值ID'"`
}

func ProductCreate(info *Product) error {
	return DB.Debug().Create(&info).Error
}

func ProductList(pages, size int) (result []*Product, err2 error) {
	page := pages
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	err2 = DB.Offset(offset).Limit(pageSize).Find(&result).Error
	if err2 != nil {
		return nil, err2
	}
	return
}
